package agent

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/rpc"
	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type GetYesterdayUserRebatesLevelListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetYesterdayUserRebatesLevelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetYesterdayUserRebatesLevelListLogic {
	return &GetYesterdayUserRebatesLevelListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetYesterdayUserRebatesLevelListLogic) GetYesterdayUserRebatesLevelList(req *types.YesterdayUserRebatesLevelReq) (resp *types.YesterdayUserRebatesLevelList, err error) {
	// 昨日返佣等级--列表
	rpcReq := rpc.YesterdayRebatesLevelListReq{
		Level:                 req.Level,
		UserId:                req.UserId,
		MinTeamHeadCount:      req.MinTeamHeadCount,
		MaxTeamHeadCount:      req.MaxTeamHeadCount,
		MinTeamRechargeAmount: req.MinTeamRechargeAmount,
		MaxTeamRechargeAmount: req.MaxTeamRechargeAmount,
		MinGameAmount:         req.MinGameAmount,
		MaxGameAmount:         req.MaxGameAmount,
		QueryDate:             req.QueryDate,
		Order:                 req.Order,
		// Sort:                  req.Sort,
		Page: req.Page,
		Size: req.Size,
	}
	// Sort:                  req.Sort,
	rpcResp, err := l.svcCtx.AgentRPC.GetYesterdayRebatesLevelList(context.Background(), &rpcReq)
	if err != nil {
		logc.Error(l.ctx, err)
		return nil, err
	}

	_data := make([]types.YesterdayUserRebatesLeveInfo, len(rpcResp.List))
	for _i, _row := range rpcResp.List {
		_detail := make([]types.GameRebatesInfo, len(_row.Detail))
		for _j, _v := range _row.Detail {
			_detail[_j] = types.GameRebatesInfo{
				Type:          _v.Type,
				Name:          _v.Name,
				Amount:        _v.Amount,
				RebatesAmount: _v.RebatesAmount,
			}
		}
		_data[_i] = types.YesterdayUserRebatesLeveInfo{
			Id:                         _row.Id,
			Date:                       _row.Date,
			UserId:                     _row.UserId,
			TeamHeadCount:              _row.TeamHeadCount,
			TeamRechargeAmount:         _row.TeamRechargeAmount,
			GameAmount:                 _row.GameAmount,
			Level:                      _row.Level,
			Detail:                     _detail,
			ComputationalRebatesAmount: _row.ComputationalRebatesAmount,
			ProvideRebatesAmount:       _row.ProvideRebatesAmount,
			ProvideAt:                  _row.ProvideAt,
		}
	}

	resp = &types.YesterdayUserRebatesLevelList{
		Page:      int32(rpcResp.Page),
		PageSize:  int32(rpcResp.PageSize),
		Total:     rpcResp.Total,
		TotalPage: rpcResp.TotalPage,
		List:      _data,
	}

	return
}
